﻿Public Class frmPHIBR
    Private frmParent As frmMain
    Public lstview As ListView = lvwPHI
    Dim validator As PropertyValidator = New PropertyValidator
    'Dim sqlManager As New SQLManager
    'Dim sqlList As New SQLList
    Dim payrollManager As New PayrollManager
    Private Sub frmPHIBR_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.MdiParent = frmMain
        ClearAllFields()

        Try
            'Dim phiList As List(Of PhilHealth) = sqlList.getAllPHI()
            Dim phiList As List(Of PhilHealth) = payrollManager.getAllPHI()
            For Each phi As PhilHealth In phiList
                Dim item As ListViewItem
                item = lvwPHI.Items.Add(phi.bracket)
                item.SubItems.Add(phi.min_salary)
                item.SubItems.Add(phi.max_salary)
                item.SubItems.Add(phi.base_salary)
                item.SubItems.Add(phi.employer_share)
                item.SubItems.Add(phi.employee_share)
                item.SubItems.Add(phi.id)
            Next
            grpPHI.Text = "Total Record(s): " + lvwPHI.Items.Count.ToString()
            validator.resizes(lvwPHI)
        Catch ex As Exception
            MsgBox("Can't load Web page" & vbCrLf & ex.Message)
        End Try
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        frmPHIDE.ShowDialog()
        frmPHIBR_Load(vbNull, EventArgs.Empty)
    End Sub

    Public Function ClearAllFields()
        validator.ResetControls(grpPHI)
        btnAdd.Enabled = True
        btnEdit.Enabled = False
        btnDelete.Enabled = False
        btnImport.Enabled = True
        grpPHI.Text = "Total Record(s): 0"
        validator.resizes(lvwPHI)
        Clipboard.Clear()
        Return 0
    End Function


    Private Sub lvwPHI_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lvwPHI.SelectedIndexChanged
        btnEdit.Enabled = (lvwPHI.SelectedItems.Count = 1)
        btnDelete.Enabled = (lvwPHI.SelectedItems.Count > 0)
    End Sub

    Private Sub btnImport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnImport.Click
        Clipboard.SetText("PHI")
        frmImport.ShowDialog()
        frmPHIBR_Load(vbNull, EventArgs.Empty)
    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        Clipboard.SetText(lvwPHI.SelectedItems(0).SubItems(6).Text)
        frmPHIDE.ShowDialog()
        frmPHIBR_Load(vbNull, EventArgs.Empty)
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        Try
            For i As Integer = 1 To lvwPHI.SelectedItems.Count
                'sqlManager.executeQuery("DELETE FROM tbl_philhealth WHERE id = " + lvwPHI.SelectedItems(i - 1).SubItems(6).Text)
                payrollManager.deleteItemPHI(lvwPHI.SelectedItems(i - 1).SubItems(6).Text)
            Next
            MsgBox("Record(s) has been deleted.")
            frmPHIBR_Load(vbNull, EventArgs.Empty)
            grpPHI.Text = "Total Record(s): " + lvwPHI.Items.Count.ToString()
        Catch ex As Exception
            MsgBox("Can't load Web page" & vbCrLf & ex.Message)
        End Try
    End Sub
End Class